package com.ruoyi.order.task;

import com.ruoyi.order.websocket.StatisticsWebSocketHandler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Component
public class StatisticsPushTask {

    // 每5秒推送一次
    @Scheduled(fixedRate = 5000)
    public void pushStatistics() {

        // 随机生成订单数、任务数、用户数
        int orderCount = (int) (Math.random() * 1000);
        int taskCount = (int) (Math.random() * 100);
        int userCount = (int) (Math.random() * 1000);

        // 随机生成商户数、新增用户数、累计用户数
        int merchantCount = (int) (Math.random() * 100);
        int newUserCount = (int) (Math.random() * 100);
        int totalUserCount = (int) (Math.random() * 1000);

        // 随机生成今日订单总额（元），今日订单总数，今日新增商户
        double todayOrderTotal = (int) (Math.random() * 1000 + 1000);
        int todayOrderCount = (int) (Math.random() * 100);
        int todayMerchantCount = (int) (Math.random() * 100);

        Map<String, Object> data = new HashMap<>();
        data.put("orderPie", Arrays.asList(
                // 使用 HashMap 替代 Map.of
                new HashMap<String, Object>() {{
                    put("value", orderCount);
                    put("name", "维修类");
                }},
                new HashMap<String, Object>() {{
                    put("value", taskCount);
                    put("name", "洗车类");
                }},
                new HashMap<String, Object>() {{
                    put("value", userCount);
                    put("name", "保养类");
                }}
        ));
        data.put("summaryList", Arrays.asList(
                new HashMap<String, Object>() {{
                    put("value", todayOrderTotal);
                    put("name", "今日订单总额（元）");
                }},
                new HashMap<String, Object>() {{
                    put("value", todayOrderCount);
                    put("name", "今日订单总数");
                }},
                new HashMap<String, Object>() {{
                    put("title", "今日新增用户数");
                    put("value", newUserCount);
                }},
                new HashMap<String, Object>() {{
                    put("title", "今日新增商户");
                    put("value", todayMerchantCount);
                }}
        ));

        // 随机生成待处理任务数、已处理任务数
        int pendingTaskCount = (int) (Math.random() * 100);
        int completedTaskCount = (int) (Math.random() * 100);

        data.put("taskPie", Arrays.asList(
                // 使用 HashMap 替代 Map.of
                new HashMap<String, Object>() {{
                    put("value", pendingTaskCount);
                    put("name", "待处理");
                }},
                new HashMap<String, Object>() {{
                    put("value", completedTaskCount);
                    put("name", "已处理");
                }}
        ));

        data.put("userSummary", Arrays.asList(
                new HashMap<String, Object>() {{
                    put("title", "商户数");
                    put("value", merchantCount);
                }},
                new HashMap<String, Object>() {{
                    put("title", "新增用户数");
                    put("value", newUserCount);
                }},
                new HashMap<String, Object>() {{
                    put("title", "累计用户数");
                    put("value", totalUserCount);
                }}
        ));
        StatisticsWebSocketHandler.broadcast(data);
    }
}
